<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\tool\Result;
use app\common\model\User;
use think\exception\ValidateException;
use think\Response;

class Security extends Base
{
    public function index($type = 'pay_password'): Response
    {
        $is_set = 1;
        if (!$this->user->$type) {
            $is_set = 0;
        }

        return Result::success('success', ['is_set' => $is_set]);
    }

    public function save(): Response
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'Security');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }
        if (empty($param['type'])) {
            $param['type'] = 'pay_password';
        }
        $type = $param['type'];
        $first = false;
        if (!$this->user->$type) {
            $first = true;
        }

        return Result::fastReturn(User::security($param, $this->user, $first));
    }
}